<?php
//档案管理
namespace Home\Controller;

use Think\Controller;

class BespeakController extends Controller
{
    public function _initialize()
    {
        if (empty($_SESSION['empid'])) {
            $this->error('页面加载中。。。', U('Login/Login'), 1);
        }
    }

    public function Index()
    {        //获取周一的日期
        if (I('get.fs') == 'down'&&!IS_POST) {

            $start = strtotime(I('get.sd')) + (7 * 24 * 60 * 60);
            $sd = date('Y-m-d', $start);
            $end = $start + (6 * 24 * 60 * 60);
            $ts = 7;
        } elseif (I('get.fs') == 'up'&&!IS_POST) {

            $start = strtotime(I('get.sd')) - (7 * 24 * 60 * 60);
            $sd = date('Y-m-d', $start);
            $end = $start + (6 * 24 * 60 * 60);
            $ts = 7;
        } else {

            if (IS_POST) {
                //查询条件
                $start_time = I('post.start_time');
                //var_dump($start_time);
                $end_time = I('post.end_time');
//                dump($end_time);exit;
                if ($start_time > $end_time or empty($start_time) or empty($end_time)) {
                    $this->error('时间区间选择有误');
                }
                $start = strtotime($start_time);
                $end = strtotime($end_time);
                $ts = (($end - $start) / 86400) + 1;
            } else {
                //var_dump(111);
                $week = date('w');
                if ($week == 1) {
                    $start_time = date('Y-m-d');
                } else {
                    $start_time = date('Y-m-d', strtotime('last monday'));
                }
                $start = strtotime($start_time);
                $sd = $start_time;
                $end = $start + 86400*6;
                $ts = 7;
            }
        }

        $start_time = date('Y-m-d', $start);
        $end_time = date('Y-m-d', $end);
        header("Content-type:text/html;charset=utf-8");

       
        //获取课程
        for ($i = 0; $i < $ts; $i++) {
         

            $riqi = date('Y-m-d', $start);
            $sriqi=date('Y-m-d',$start+86400);
            $map['start_time'] = $riqi;
            $arrCourse[$riqi] = M('CCourse')->where($map)->order('start_time asc')->select();
           // $mab['start_time'] = array('gt',$riqi);
            // $mab['start_time'] = array('between',''.$riqi.','.$sriqi.'');

            // $skc=M('CCourse')->where($mab)->field('schedule_id')->select();
        
            // var_dump($sriqi);
            //求出每天人数/单间数/标间数
            //-------------------------------

            $a = M('CCourse')->where($map)->distinct(true)->field('schedule_id,start_time,courseid')->select();
            $peopleNum = 0;
            $singleRoomNum = 0;
            $standardRoomNum = 0;
            foreach ($a as $item => $v2){
                $skc=M('CCourse')->where(array('courseid'=>$v2['courseid']))->order('start_time desc')->field('start_time')->find();
               // var_dump($skc);
               if($skc['start_time']!==$v2['start_time']){
               
                 $id['id'] = $v2['schedule_id'];
                 $peopleNum += M('CSchedule')->where($id)->getField('peoplenum');

                 $singleRoomNum += M('CSchedule')->where($id)->getField('singleroomnum');
                 $standardRoomNum += M('CSchedule')->where($id)->getField('standardroomnum');
              }  
            }
            
           
            // $a = implode(',', $a);

            // $peopleNum = 0;
            // $singleRoomNum = 0;
            // $standardRoomNum = 0;
            // if($a) {
            //     $id['id'] = array('in', $a);
            //     $peopleNum = M('CSchedule')->where($id)->sum('peoplenum');
            //     $singleRoomNum = M('CSchedule')->where($id)->sum('singleroomnum');
            //     $standardRoomNum = M('CSchedule')->where($id)->sum('standardroomnum');
            // }
            $arrCourse[$riqi][peoplenum] = $peopleNum;
            $arrCourse[$riqi][singleroomnum] = $singleRoomNum;
            $arrCourse[$riqi][standardroomnum] = $standardRoomNum;
            //var_dump($arrCourse);
            //-------------------------------------
            $start += 86400;
        }
        //var_dump($start_time);
        //获取班次
        $where['start_time'] = array('between', '' . $start_time . ',' . $end_time . '');
        $ba = M('CCourse')->where($where)->order('start_time asc')->Distinct(true)->select();
        foreach ($ba as $item => $value) {
            $m[] = $value['schedule_id'];
        }
        $m=array_unique($m);
        $m = implode(',', $m);

        $arrSchedule = array();
        if($m){
            $id['id'] = array('in', $m);
            $arrSchedule = M('CSchedule')->where($id)->order('startdate asc,noontype1 asc ')->select();
            //人数
            $sumPeople = M('CSchedule')->where($id)->field('SUM(peoplenum)')->select();
     
            //单间数
            $sumSingle = M('CSchedule')->where($id)->field('SUM(singleroomnum)')->select();
            //标间数
            $sumStandard = M('CSchedule')->where($id)->field('SUM(standardroomnum)')->select();
        }
      
        header("Content-type:text/html;charset=utf-8");
//        dump($arrCourse);exit;
        //接班老师
        $arrJTeacher = M('CTeacher')->where("type = 1")->select();
        //带班老师
        $arrDTeacher = M('CTeacher')->where("type = 2")->select();
        //值班领导
        $arrLeader = M('CTeacher')->where("type = 3")->select();

        $start_time = date('m-d', strtotime($start_time) );
        $end_time = date('m-d', strtotime($end_time) );

        $class = R('Pub/Classs');
        $this->assign('class', $class);
        $this->assign('arrJTeacher', $arrJTeacher);
        $this->assign('arrDTeacher', $arrDTeacher);
        $this->assign('arrLeader', $arrLeader);
        $this->assign('start_time', $start_time);
//        $this->assign('start_time1', $start_time1);
        $this->assign('sd', $sd);
        $this->assign('end_time', $end_time);
        $this->assign('arrSchedule', $arrSchedule);
        $this->assign('arrCourse', $arrCourse);
        $this->assign('sumPeople', $sumPeople);
        $this->assign('sumSingle', $sumSingle);
        $this->assign('sumStandard', $sumStandard);
        $this->display();
    }

    //预约课程页面
    public function Detail()
    {
        $cf['id'] = I('get.id');
        $type = I('type');
        if($type == 1){
            $type = "disabled";
        }
        $arrData = array();
        if ($cf['id']) {
            $arrData = M('CSchedule')->where($cf)->find();
        }
        //接班老师
        $arrJTeacher = M('CTeacher')->where("type = 1")->select();
        //带班老师
        $arrDTeacher = M('CTeacher')->where("type = 2")->select();
        //值班领导
        $arrLeader = M('CTeacher')->where("type = 3")->select();
        $this->assign('arrData', $arrData);
        $this->assign('arrJTeacher', $arrJTeacher);
        $this->assign('arrDTeacher', $arrDTeacher);
        $this->assign('arrLeader', $arrLeader);
        $this->assign('type', $type);
        $this->display();
    }

    //修改
    public function Edit()
    {
        $strId = I('post.id');
        $schedule = M('CSchedule');
        $schedule->create();
//        print_r(I('post.standardroomnum'));exit;
        //预约编号不能重复，判断是否存在预约
        if(I('post.scheid')){
            if ($strId) {
                $cf['scheid'] = array('in', I('post.scheid'));
                $cf['id'] = array('not in', $strId);
            } else {
                $cf['scheid'] = I('post.scheid');
            }
//        print_r($cf);exit;
            $arrData = M('CSchedule')->where($cf)->select();
//        echo M('CSchedule')->getLastSql();exit;
            if (!empty($arrData)) {
                echo '已有此预约班次信息';
                exit;
            }
        }

        if (I('post.startdate') > I('post.enddate')) {
            echo '结束时间必须大于开始时间';
            exit;
        }
        //主键存在保存，否则新增
        if ($strId) {
            if(I('post.oldstartdate') != I('post.startdate') ||
                I('post.oldnoontype1') != I('post.noontype1') ||
                I('post.oldenddate') != I('post.enddate') ||
                I('post.oldnoontype2') != I('post.noontype2')){
                $arrData = M('CCourse')->where("schedule_id = $strId")->field('id')->select();
                foreach ($arrData as $k => $v){
                    $m[] = $v['id'];
                }
                $id['id'] = array('in', $m);
                $status = M('CCourse')->where($id)->delete();
                $startTime = strtotime(I('post.startdate'));
                //结束日期
                $endTime = strtotime(I('post.enddate'));
                $noontype1 = I('post.noontype1');
                $noontype2 = I('post.noontype2');
//          中间有几天
                $middleCount = ($endTime - $startTime) / 86400;
//            print_r(date($startTime));exit;
//            因为中途添加中午，上中下晚 分别为1423，为了排序 没用for循环
                for ($i = 1; $i < $middleCount; $i++) {
                    $date33 = $startTime + 86400 * $i;
//                for ($j = 1; $j < 5; $j++){
//                    $m = D(CCourse);
//                    $m->courseid = I('post.scheid');
//                    $m->coursename = "待定";
//                    $m->start_time = date('Y-m-d', $date33);
//                    $m->is_am = $j;
//                    $m->add();
//                }
                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "待定";
                    $m->start_time = date('Y-m-d', $date33);
                    $m->is_am = 1;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "学院就餐";
                    $m->start_time = date('Y-m-d', $date33);
                    $m->is_am = 4;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "待定";
                    $m->start_time = date('Y-m-d', $date33);
                    $m->is_am = 2;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "待定";
                    $m->start_time = date('Y-m-d', $date33);
                    $m->is_am = 3;
                    $m->add();
                }
//            计算第一天增加条数
//                print_r($noontype1);exit;
                if ($noontype1 == 1) {
                    //上午
                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "报到";
                    $m->start_time = I('post.startdate');
                    $m->is_am = 1;
                    $m->add();

                    $d = D(CCourse);
                    $d->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $d->coursename = "学院就餐";
                    $d->start_time = I('post.startdate');
                    $d->is_am = 4;
                    $d->add();

                    $d = D(CCourse);
                    $d->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $d->coursename = "待定";
                    $d->start_time = I('post.startdate');
                    $d->is_am = 2;
                    $d->add();

                    $d = D(CCourse);
                    $d->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $d->coursename = "待定";
                    $d->start_time = I('post.startdate');
                    $d->is_am = 3;
                    $d->add();


                } elseif ($noontype1 == 2) {
//                下午
                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "报到";
                    $m->start_time = I('post.startdate');
                    $m->is_am = 2;
                    $m->add();

                    $d = D(CCourse);
                    $d->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $d->coursename = "待定";
                    $d->start_time = I('post.startdate');
                    $d->is_am = 3;
                    $d->add();
                } elseif ($noontype1 == 3) {
//                晚上
                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "报到";
                    $m->start_time = I('post.startdate');
                    $m->is_am = 3;
                    $m->add();
                } elseif ($noontype1 == 4) {
//                中午
                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "学院就餐";
                    $m->start_time = I('post.startdate');
                    $m->is_am = 4;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "待定";
                    $m->start_time = I('post.startdate');
                    $m->is_am = 2;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "待定";
                    $m->start_time = I('post.startdate');
                    $m->is_am = 3;
                    $m->add();
                }
                //计算最后一天增加条数
                if ($noontype2 == 1) {
//                上午
                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "返程";
                    $m->start_time = I('post.enddate');
                    $m->is_am = 1;
                    $m->add();
                } elseif ($noontype2 == 2) {
//                下午
                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "待定";
                    $m->start_time = I('post.enddate');
                    $m->is_am = 1;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "学院就餐";
                    $m->start_time = I('post.enddate');
                    $m->is_am = 4;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "返程";
                    $m->start_time = I('post.enddate');
                    $m->is_am = 2;
                    $m->add();
                } elseif ($noontype2 == 3) {
//                晚上
                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "待定";
                    $m->start_time = I('post.enddate');
                    $m->is_am = 1;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "学院就餐";
                    $m->start_time = I('post.enddate');
                    $m->is_am = 4;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "待定";
                    $m->start_time = I('post.enddate');
                    $m->is_am = 2;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "返程";
                    $m->start_time = I('post.enddate');
                    $m->is_am = 3;
                    $m->add();
                } elseif ($noontype2 == 4) {
//                中午
                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "待定";
                    $m->start_time = I('post.enddate');
                    $m->is_am = 1;
                    $m->add();

                    $m = D(CCourse);
                    $m->courseid = I('post.scheid');
                    $m->schedule_id = $strId;
                    $m->coursename = "学院就餐";
                    $m->start_time = I('post.enddate');
                    $m->is_am = 4;
                    $m->add();
                }
            }
            $updata = $schedule->save();
            //添加日志
            $m = D(CLog);
            $m->operate_user = $_SESSION['loginname'];
            $m->operate_time = date('Y-m-d h:i:s',time());
            $m->operate_module = "预约班次";
            $m->operate = "编辑预约班次";
            $m->name = I('post.schename');
            $m->number = I('post.scheid');
            $m->add();
        } else {
            $updata = $schedule->add();
            $m = D(CLog);
            $m->operate_user = $_SESSION['loginname'];
            $m->operate_time = date('Y-m-d h:i:s',time());
            $m->operate_module = "预约班次";
            $m->operate = "新增预约班次";
            $m->name = I('post.schename');
            $m->number = I('post.scheid');
            $m->add();

            $strId = $updata;

            $startTime = strtotime(I('post.startdate'));
            //结束日期
            $endTime = strtotime(I('post.enddate'));
            $noontype1 = I('post.noontype1');
            $noontype2 = I('post.noontype2');
//          中间有几天
            $middleCount = ($endTime - $startTime) / 86400;
//            print_r(date($startTime));exit;
//            因为中途添加中午，上中下晚 分别为1423，为了排序 没用for循环
            for ($i = 1; $i < $middleCount; $i++) {
                $date33 = $startTime + 86400 * $i;
//                for ($j = 1; $j < 5; $j++){
//                    $m = D(CCourse);
//                    $m->courseid = I('post.scheid');
//                    $m->coursename = "待定";
//                    $m->start_time = date('Y-m-d', $date33);
//                    $m->is_am = $j;
//                    $m->add();
//                }
                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "待定";
                $m->start_time = date('Y-m-d', $date33);
                $m->is_am = 1;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "学院就餐";
                $m->start_time = date('Y-m-d', $date33);
                $m->is_am = 4;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "待定";
                $m->start_time = date('Y-m-d', $date33);
                $m->is_am = 2;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "待定";
                $m->start_time = date('Y-m-d', $date33);
                $m->is_am = 3;
                $m->add();
            }
//            计算第一天增加条数
            if ($noontype1 == 1) {
                //上午
                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "报到";
                $m->start_time = I('post.startdate');
                $m->is_am = 1;
                $m->add();

                $d = D(CCourse);
                $d->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $d->coursename = "学院就餐";
                $d->start_time = I('post.startdate');
                $d->is_am = 4;
                $d->add();

                $d = D(CCourse);
                $d->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $d->coursename = "待定";
                $d->start_time = I('post.startdate');
                $d->is_am = 2;
                $d->add();

                $d = D(CCourse);
                $d->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $d->coursename = "待定";
                $d->start_time = I('post.startdate');
                $d->is_am = 3;
                $d->add();


            } elseif ($noontype1 == 2) {
//                下午
                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "报到";
                $m->start_time = I('post.startdate');
                $m->is_am = 2;
                $m->add();

                $d = D(CCourse);
                $d->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $d->coursename = "待定";
                $d->start_time = I('post.startdate');
                $d->is_am = 3;
                $d->add();
            } elseif ($noontype1 == 3) {
//                晚上
                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "报到";
                $m->start_time = I('post.startdate');
                $m->is_am = 3;
                $m->add();
            } elseif ($noontype1 == 4) {
//                中午
                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "学院就餐";
                $m->start_time = I('post.startdate');
                $m->is_am = 4;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "待定";
                $m->start_time = I('post.startdate');
                $m->is_am = 2;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "待定";
                $m->start_time = I('post.startdate');
                $m->is_am = 3;
                $m->add();
            }
            //计算最后一天增加条数
            if ($noontype2 == 1) {
//                上午
                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "返程";
                $m->start_time = I('post.enddate');
                $m->is_am = 1;
                $m->add();
            } elseif ($noontype2 == 2) {
//                下午
                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "待定";
                $m->start_time = I('post.enddate');
                $m->is_am = 1;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "学院就餐";
                $m->start_time = I('post.enddate');
                $m->is_am = 4;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "返程";
                $m->start_time = I('post.enddate');
                $m->is_am = 2;
                $m->add();
            } elseif ($noontype2 == 3) {
//                晚上
                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "待定";
                $m->start_time = I('post.enddate');
                $m->is_am = 1;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "学院就餐";
                $m->start_time = I('post.enddate');
                $m->is_am = 4;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "待定";
                $m->start_time = I('post.enddate');
                $m->is_am = 2;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "返程";
                $m->start_time = I('post.enddate');
                $m->is_am = 3;
                $m->add();
            } elseif ($noontype2 == 4) {
//                中午
                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "待定";
                $m->start_time = I('post.enddate');
                $m->is_am = 1;
                $m->add();

                $m = D(CCourse);
                $m->courseid = I('post.scheid');
                $m->schedule_id = $strId;
                $m->coursename = "学院就餐";
                $m->start_time = I('post.enddate');
                $m->is_am = 4;
                $m->add();
            }

        }
        if ($updata) {
            $data = 0;
        } else {
            $data = 1;
        }
        echo $data;
    }

    //删除预约信息
    public function Del()
    {
        $map['id'] = I('get.id');
//        print_r(I('get.id'));exit;
        $schedule = M('CSchedule')->where($map)->select();
        $schename = $schedule[0]['schename'];
        $scheid = $schedule[0]['scheid'];
        $strId = I('get.id');
        $delCourse = M('CCourse')->where("schedule_id = $strId")->delete();
        $del = M('CSchedule')->where($map)->delete();

        if ($del) {
            $m = D(CLog);
            $m->operate_user = $_SESSION['loginname'];
            $m->operate_time = date('Y-m-d h:i:s',time());
            $m->operate_module = "预约班次";
            $m->operate = "删除预约班次";
            $m->name = $schename;
            $m->number = $scheid;
            $m->add();
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

//    批量保存
    public function SaveCourse(){
        $course = M('CCourse');
        foreach ($_POST as $item =>$value){
            $data['coursename'] = $value;
            if(is_numeric($item)){
                $list = $course->where("id = $item")->save($data);
            }
        }
        $data = 0;
        echo $data;
    }

    //    批量保存
    public function SaveSchedule(){
        $schedule = M('CSchedule');
        $id = $_POST['id'];
//        $scheid = $_POST['scheid'];
//        if($scheid){
//            if ($id) {
//                $cf['scheid'] = array('in', $scheid);
//                $cf['id'] = array('not in', $id);
//            } else {
//                $cf['scheid'] = $scheid;
//            }
//            $arrData = M('CSchedule')->where($cf)->select();
//            if (!empty($arrData)) {
//                $this->error('保存失败');
//                exit;
//            }
//        }
        $list = $schedule->where("id = $id")->save($_POST);
    }


    //    导入信息
    public function ImportExcel(){
        $upload = new \Think\Upload(); // 实例化上传类
        $upload-> maxSize = 3145728; // 设置附件上传大小
        $upload-> exts = array('xls','xlsx'); // 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (! $info) { // 上传错误提示错误信息
            $this->error($upload->getError());
        } else {
            $file_name = "./Uploads/" . $info['photo']['savepath'] . $info['photo']['savename'];
            $ext = $info['photo']['ext'];
        }
//        header("Content-type:text/html;charset=utf-8");
//        dump($info['photo']);exit;



        import("Common.Org.PHPExcel");
        import("Common.Org.PHPExcel.Writer.Excel5");
        import("Common.Org.PHPExcel.IOFactory.php");
        import("Common.Org.PHPExcel.Reader.Excel5");
        $objPHPExcel = new \PHPExcel();
        if ($ext == "xlsx") {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        } else {
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        }
        $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
//        $sheet = $objPHPExcel->getSheet(0);
//        $highestRow = $sheet->getHighestRow(); // 取得总行数
//        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
//        $arrExcel = $objPHPExcel->getSheet(0)->toArray();
//        echo 11111111111;exit;
        $data['scheid'] = $objPHPExcel->getActiveSheet()->getCell("K2")->getValue();
        $data2['courseid'] = $objPHPExcel->getActiveSheet()->getCell("K2")->getValue();
        $data['jteacher'] = $objPHPExcel->getActiveSheet()->getCell("K3")->getValue();
        $data['dteacher'] = $objPHPExcel->getActiveSheet()->getCell("K4")->getValue();
        header("Content-type:text/html;charset=utf-8");
//        print_r($data);exit;
//        print_r($_GET[id]);exit;
        $listSchedule = M('CSchedule')->where("id = $_GET[id]")->save($data);
        $listCourse = M('CCourse')->where("schedule_id = $_GET[id]")->save($data2);

        if ($listSchedule) {
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }
    }

    //课程列表
    public function Courselist()
    {
//        print_r(I('get.scheid'));exit;
        $cf['schedule_id'] = I('get.id');
        $list = array();
        if ($cf['schedule_id']) {
            $list = M('CCourse')->where($cf)->order("start_time, id asc")->select();
        }
        //教室
        $arrClass = M('CClass')->select();
        //教师
        $arrTeacher = M('CTeacher')->select();
        header("Content-type:text/html;charset=utf-8");
        $schedule_id = I('get.id');
        $course_id = I('get.scheid');
//        print_r($course_id);exit;
//        $this->assign([
//            'list' => $list,
//            'arrClass' => $arrClass,
//            'arrTeacher' => $arrTeacher,
//            'schedule_id' => $schedule_id,
//        ]);
        $this->assign('list', $list);
        $this->assign('arrClass', $arrClass);
        $this->assign('arrTeacher', $arrTeacher);
        $this->assign('schedule_id', $schedule_id);
        $this->assign('course_id', $course_id);
        $this->display();
    }

    public function Courseadd(){
        //获取最新添加一条的日期start_time
//        print_r(I('get.course_id'));exit;
        $cf['schedule_id'] = I('get.schedule_id');

        $arrData = M('CCourse')->order('id desc')->where($cf)->find();
//        print_r($arrData);exit;

        $m = D(CCourse);
        $m->schedule_id = I('get.schedule_id');
        $m->courseid = I('get.course_id');
        $m->coursename = "待定";
        $m->start_time = $arrData['start_time'];
        $m->is_am = 1;
        $m->remark = "新增数据";
        $m->add();
        echo "<script>location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
    }


    //课程页面修改课程信息
    public function CourseEdit()
    {
        $user = M('CCourse');
        $list = $user->where("id = $_POST[id]")->save($_POST);

        if ($list) {
            $data = 0;
        }
        echo $data;
    }

    //删除预约课程信息
    public function CourseDel()
    {
        $map['id'] = I('get.id');
//        print_r(I('get.id'));exit;
        $del = M('CCourse')->where($map)->delete();
        if ($del) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //修改课程页面
    public function Detail2()
    {
        $cf['id'] = I('get.id');
        $arrData = array();
        if ($cf['id']) {
            $arrData = M('CCourse')->where($cf)->find();
        }
        //班次名称
        $arrClass = M('CClass')->select();
        //教师
        $arrTeacher = M('CTeacher')->select();
//        $this->assign([
//            'arrData' => $arrData,
//            'arrClass' => $arrClass,
//            'arrTeacher' => $arrTeacher,
//        ]);
        $this->assign('arrData', $arrData);
        $this->assign('arrClass', $arrClass);
        $this->assign('arrTeacher', $arrTeacher);
        $this->display();
    }

    //修改课程信息
    public function Edit2()
    {
        $m = D(CCourse);
        $m->save($_POST);
        if ($m) {
            $data = 0;
        } else {
            $data = 1;
        }
        echo $data;
    }

    /**
     * -------------------------------------------------------------------------------------由于需求变动，下面方法未用到
     */
    public function Index777()
    {
        //获取周一的日期
        if (I('get.fs') == 'down') {
            $start_time = strtotime(I('get.sd')) + (7 * 24 * 60 * 60);
            $end_time = $start_time + (6 * 24 * 60 * 60);
            $ts = 7;
        } elseif (I('get.fs') == 'up') {
            $start_time = strtotime(I('get.sd')) - (7 * 24 * 60 * 60);
            $end_time = $start_time + (6 * 24 * 60 * 60);
            $ts = 7;
        } else {
            if (IS_POST) {
                //查询条件
                $start_time = I('post.start_time');
                $end_time = I('post.end_time');
                if ($start_time > $end_time or empty($d) or empty($en)) {
                    $this->error('时间区间选择有误');
                }
                $start = strtotime($start_time);
                $end = strtotime($end_time);
                $ts = (($end - $start) / 86400) + 1;
            } else {
                $week = date('w');
                if ($week == 1) {
                    $start_time = date('Y-m-d');
                } else {
                    $start_time = date('Y-m-d', strtotime('last monday'));
                }
                $start = strtotime($start_time);
                $end_time = date('Y-m-d',$start + 86400*6);
                $ts = 7;
            }
        }
        //获取课程
        for ($i = 0; $i < $ts; $i++) {
            $riqi = date('Y-m-d', $start);
            $map['start_time'] = $riqi;
            $arrCourse[$riqi] = M('CCourse')->where($map)->order('start_time asc')->select();
            $start += 86400;
        }
        //获取班次
        $where['start_time'] = array('between', '' . $start_time . ',' . $end_time . '');
        $ba = M('CCourse')->where($where)->order('start_time asc')->Distinct(true)->select();
        foreach ($ba as $item => $value) {
            $m[] = $value['courseid'];
        }
        $m = implode(',', $m);
        $arrSchedule = array();
        if($m){
            $scheid['scheid'] = array('in', $m);
            $arrSchedule = M('CSchedule')->where($scheid)->order('startdate asc')->select();
        }
        $class = R('Pub/Classs');
        $this->assign('class', $class);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('arrSchedule', $arrSchedule);
        $this->assign('arrCourse', $arrCourse);
        $this->display();
    }

    public function Edit3()
    {
//        print_r($_POST);exit;
        $m = D(CCourse);
        $m->add($_POST);
        if ($m) {
            $data = 0;
        } else {
            $data = 1;
        }
        echo $data;
    }



    public function index666()
    {
//        var_dump(IS_POST);exit;
//        echo THINK_VERSION;EXIT;
        if (IS_POST) {
            $stime2 = I('post.start_time');
            $etime2 = I('post.end_time');
        } else {
            if (I('get.fs') == 'down') {
                $stime = I('get.sd') + (7 * 24 * 60 * 60);
            } elseif (I('get.fs') == 'up') {
                $stime = I('get.sd') - (7 * 24 * 60 * 60);
            } else {
                $week = date('w');
                if ($week == 1) {
                    $stime = strtotime(date('Y-m-d'));
                } else {
                    $stime = strtotime('last monday');
                }
            }
            $etime = $stime + (6 * 24 * 60 * 60);
            $stime2 = date('Y-m-d', $stime);
            $etime2 = date('Y-m-d', $etime);
        }
        $where['start_time'] = array('between', '' . $stime2 . ',' . $etime2 . '');
        //查询课程
        $ba = M('CCourse')->where($where)->Distinct(true)->field('courseid')->select();
        header("Content-type:text/html;charset=utf-8");
//        dump($ba);exit;
        //查询班次
        foreach ($ba as $item => $value) {
            $m[] = $value['courseid'];
        }
        $m = implode(',', $m);
//        dump($m);exit;
        $arrBespeak = array();
        if ($m) {
            $SQL = "SELECT a.schename,a.scheid,a.jteacher,a.dteacher,b.bespeak_id,b.people_num,b.single_room_num,b.standard_room_num,b.mark,b.classes_num
                FROM gb_c_bespeak AS b
                LEFT JOIN gb_c_schedule AS a
                ON a.scheid = b.classes_num
                WHERE a.scheid IN ($m) ";
            $arrBespeak = M('CBespeak')->query($SQL);
        }

        if (IS_POST) {
            $star_time = I('post.start_time');
            $end_time = I('post.end_time');
            $d = strtotime($star_time);
            $en = strtotime($end_time);
            if ($d > $en or empty($d) or empty($en)) {
                $this->error('时间区间选择有误');
            } else {
                $ts = (($en - $d) / 86400) + 1;
                for ($i = 0; $i < $ts; $i++) {
                    $riqi = date('Y-m-d', $d);
                    $star_time = date('Y-m-d', $d);
                    $end_time = date('Y-m-d', $d);
                    $map['start_time'] = array('between', '' . $star_time . ',' . $end_time . '');
                    $sq[1][$riqi] = M('CCourse')->where($map)->select();
                    foreach ($sq[$riqi] as $k => $v) {
                        $map['scheid'] = $v['courseid'];
                        $sj = M('CSchedule')->where($map)->getField('schename');
                        $sq[1][$riqi][$k]['schename'] = $sj;
                    }
                    $d += 86400;
                }
            }
        } else {

            if (I('get.sd') != '') {
                if (I('get.fs') == 'down') {
                    $sd1 = I('get.sd');
                    $sd = $sd1 + (7 * 24 * 60 * 60);
                } elseif (I('get.fs') == 'up') {
                    $sd1 = I('get.sd');
                    $sd = $sd1 - (7 * 24 * 60 * 60);
                } elseif (I('get.fs') == 'now') {
                    $week = date('w');
                    if ($week == 1) {
                        $sd = strtotime(date('Y-m-d'));
                    } else {
                        $sd = strtotime('last monday');
                    }
                }
            } else {
                $week = date('w');
                if ($week == 1) {
                    $sd = strtotime(date('Y-m-d'));
                } else {
                    $sd = strtotime('last monday');
                }
            }
            $this->assign('sd', $sd);
            $d = $sd;

            for ($i = 0; $i < 7; $i++) {
                $riqi = date('Y-m-d', $d);
                $star_time = date('Y-m-d', $d);
                $end_time = date('Y-m-d', $d);
//                $map['start_time'] = array('between', '' . $star_time . ',' . $end_time . '');
//                $sq[1][$riqi] = M('CCourse')->where($map)->select();

                $SQL = "SELECT a.id,a.courseid,a.coursename,a.is_am,b.bespeak_id,b.classes_num
                            FROM gb_c_bespeak AS b
                            LEFT JOIN gb_c_course AS a 
                            ON a.courseid = b.classes_num
                            WHERE a.start_time >= '$star_time' AND a.start_time <= '$end_time' AND b.classes_num IS NOT NULL ";
                $sq[1][$riqi] = M('CCourse')->query($SQL);

                foreach ($sq[$riqi] as $k => $v) {
                    $map['scheid'] = $v['courseid'];
                    $sj = M('CSchedule')->where($map)->getField('schename');
                    $sq[1][$riqi][$k]['schename'] = $sj;
                }
                $d += 86400;
            }
        }

        $class = R('Pub/Classs');
//        dump($arrBespeak);exit;
//        $this->assign([
//            'class' => $class,
//            'sq' => $sq,
//            'arrBespeak' => $arrBespeak,
//            'riqi' => $riqi
//        ]);
        $this->assign('class', $class);
        $this->assign('sq', $sq);
        $this->assign('arrBespeak', $arrBespeak);
        $this->assign('riqi', $riqi);
        $this->display();
    }

    //添加预约课程页面
    public function CourseDetail()
    {
//        print_r(I('get.courseid'));
//        exit;
        $courseid = I('get.courseid');
        //班次名称
        $arrClass = M('CClass')->select();
        //教师
        $arrTeacher = M('CTeacher')->select();
        header("Content-type:text/html;charset=utf-8");
//        var_dump($arrTeacher);exit;
//        $this->assign([
//            'arrClass' => $arrClass,
//            'arrTeacher' => $arrTeacher,
//            'courseid' => $courseid,
//        ]);
        $this->assign('arrClass', $arrClass);
        $this->assign('arrTeacher', $arrTeacher);
        $this->assign('courseid', $courseid);
        $this->display();
    }







    public function get_week($date)
    {
        //强制转换日期格式
        $date_str = date('Y-m-d', strtotime($date));

        //封装成数组
        $arr = explode("-", $date_str);

        //参数赋值
        //年
        $year = $arr[0];

        //月，输出2位整型，不够2位右对齐
        $month = sprintf('%02d', $arr[1]);

        //日，输出2位整型，不够2位右对齐
        $day = sprintf('%02d', $arr[2]);

        //时分秒默认赋值为0；
        $hour = $minute = $second = 0;

        //转换成时间戳
        $strap = mktime($hour, $minute, $second, $month, $day, $year);

        //获取数字型星期几
        $number_wk = date("w", $strap);

        //自定义星期数组
        $weekArr = array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");

        //获取数字对应的星期
        return $weekArr[$number_wk];
    }


    //put your code here
}
